import 'package:architecture/os_size.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:tolyui/tolyui.dart';

import '../../util/utils.dart';
import '../../widgets/popup.dart';
import '../../widgets/text_button.dart';
import '../../widgets/yz_text.dart';
import 'board_activate_state_controller.dart';

class BoardActivateStatePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return GetX<BoardActivateStateController>(
        init: BoardActivateStateController(),
        builder: (controller) {
          return Scaffold(
              appBar: AppBar(
                title: Text('棋盘激活状态'),
              ),
              body: Container(
                margin: EdgeInsets.only(left: 15),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Container(
                        height: 80,
                        child: Row(
                            crossAxisAlignment: CrossAxisAlignment.center,
                            children: [
                              Container(
                                  width: windowWidth * 0.4,
                                  margin: EdgeInsets.only(right: 15),
                                  child: MyTextField(
                                    controller:
                                        controller.textEditingController,
                                    hintText: '棋盘编号',
                                  )),
                              _textButton('查询', onPressed: () {
                                controller.activateStatus();
                              }),
                              Offstage(
                                  offstage: controller.status > 1,
                                  child: Text(
                                    controller.status.value == 0
                                        ? '未激活'
                                        : '已激活',
                                    style: TextStyle(
                                        fontSize: 15,
                                        fontWeight: FontWeight.bold,
                                        color: controller.status.value == 0
                                            ? Colors.redAccent
                                            : Colors.green),
                                  )),
                              Offstage(
                                  offstage: controller.status.value != 1,
                                  child: Container(
                                    margin: EdgeInsets.only(left: 20),
                                    child: Text(
                                      countries[controller.country.value - 1],
                                      style: TextStyle(
                                          color: Colors.deepPurpleAccent),
                                    ),
                                  )),
                              // _textButton('修改国籍', onPressed: () {
                              //   controller.changeCountry();
                              // }),
                              _menu(controller)
                            ])),
                    Offstage(
                        offstage: controller.error.value.isEmpty,
                        child: Text(controller.error.value,
                            style: TextStyle(color: Colors.redAccent))),
                    _historyData(controller)
                  ],
                ),
              ));
        });
  }

  _textButton(String text, {Function? onPressed}) {
    return Container(
        margin: EdgeInsets.only(left: 15, right: 20),
        alignment: Alignment.center,
        child: MyTextButton(
            text: text,
            confirm: () {
              onPressed?.call();
            },
            backgroundColor: Colors.deepPurpleAccent,
            textStyle: TextStyle(color: Colors.white)));
  }

  _historyData(BoardActivateStateController controller) {
    return Container(
        width: windowWidth * 0.4,
        margin: EdgeInsets.only(left: 2),
        height: 300,
        child: GridView.builder(
            itemCount: controller.boardHistoryData.length,
            gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                //横轴元素个数
                crossAxisCount: 5,
                childAspectRatio: 6,
                //横轴间距
                crossAxisSpacing: 5),
            itemBuilder: (BuildContext context, int index) {
              //Widget Function(BuildContext context, int index)
              return _itemLayout(index, controller);
            }));
  }

  _itemLayout(int index, BoardActivateStateController controller) {
    final item = controller.boardHistoryData[index];
    double globPositionX = 0;
    double globPositionY = 0;
    return Listener(
        onPointerMove: (movePointEvent) {},
        child: GestureDetector(
            onTap: () {
              controller.setData(item);
            },
            onLongPress: () {
              Navigator.of(Get.context!).push(PopupWindow(
                  child: Container(
                      height: 25,
                      width: 60,
                      color: Color(0xffEBE8E8),
                      child: TextButton(
                          onPressed: () {
                            Navigator.of(Get.context!).pop();
                            controller.deleteData(item);
                          },
                          child: Text(
                            '删除',
                            style: TextStyle(color: Colors.black, fontSize: 14),
                          ))),
                  left: globPositionX,
                  top: globPositionY));
            },
            onPanDown: (DragDownDetails details) {
              globPositionX = (index + 1) % 4 == 0
                  ? details.globalPosition.dx - 5
                  : details.globalPosition.dx + 4;
              globPositionY = details.globalPosition.dy + 10;
            },
            child: Text(
              item,
              style: TextStyle(color: Colors.blue, fontWeight: FontWeight.bold),
            )));
  }

  _menu(BoardActivateStateController controller) {
    return TolyDropMenu(
        width: 180,
        offsetCalculator: boxOffsetCalculator,
        decorationConfig:
            DecorationConfig(isBubble: false, backgroundColor: Colors.white),
        onSelect: (value) {
          onSelect(value, controller);
        },
        menuItems: [
          ActionMenu(const MenuMeta(router: '1', label: '中国')),
          _divider(),
          ActionMenu(const MenuMeta(router: '2', label: '韩国')),
          _divider(),
          ActionMenu(const MenuMeta(router: '3', label: '日本')),
          _divider(),
          ActionMenu(const MenuMeta(router: '4', label: '澳大利亚')),
          _divider(),
          ActionMenu(const MenuMeta(router: '5', label: '德国')),
          _divider(),
          ActionMenu(const MenuMeta(router: '6', label: '美国')),
        ],
        childBuilder: (_, ctrl, __) {
          return _textButton('修改国籍', onPressed: () {
            ctrl.open();
          });
        });
  }

  _divider() {
    return DividerMenu(height: 0.5);
  }

  onSelect(MenuMeta menu, BoardActivateStateController controller) {
    // $message.success(message: '点击了 [${menu.label}] 个菜单');
    print('label${menu.id}');
    controller.changeCountry(int.parse(menu.id));
  }
}
